import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreMemberLevelService extends BaseService<any> {
  private readonly logger = new Logger(CoreMemberLevelService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getAll
   * 对应 PHP: CoreMemberLevelService_core::getAll()
   * 逻辑类型: undefined - undefined
   */
  async getAll(site_id: number) {
    // 基于PHP真实逻辑: getAll
    // PHP原文: $cache_name = __METHOD__ . $site_id;         return cache_remember(             $cache_name,             function() use ($site_id) {                 $...
const cache_name = __METHOD__ . site_id;
        return cache_remember(
            cache_name,
            function() use (site_id) {
                const field = 'level_id, level_name, growth,site_id,level_benefits,level_gifts';
                return this.model.where([ [ 'site_id', '=', site_id ) )).field(field).order('growth asc').select().toArray(];
  }

  /**
   * clearCache
   * 对应 PHP: CoreMemberLevelService_core::clearCache()
   * 逻辑类型: undefined - undefined
   */
  async clearCache(site_id: number) {
    // 基于PHP真实逻辑: clearCache
    // PHP原文: Cache::tag(self::$cache_tag_name . $site_id)->clear();         return true;     }      /**      * 会员检测升级      * @param $site_id      * @param $member_...
Cache.tag(self.cache_tag_name . site_id).clear(];
        return true;
    }

    /**
     * 会员检测升级
     * @param site_id
     * @param member_id
     * @return true
     */
    async checkLevelUpgrade(site_id, member_id) {
        const member = this.memberService.where([ ['member_id', '=', member_id ) )).with('member_level_data').field('member_id,growth,member_level').findOrEmpty(];
        if (member.isEmpty()) return true;

        const condition = [
            ['site_id', '=', site_id],
            ['growth', '<=', member.growth ],
        ];
        if (typeof member.member_level_data) && !!member.member_level_data)) {
            condition[] = ['growth', '>', member.member_level_data.growth ];
  }

  /**
   * checkLevelUpgrade
   * 对应 PHP: CoreMemberLevelService_core::checkLevelUpgrade()
   * 逻辑类型: undefined - undefined
   */
  async checkLevelUpgrade(site_id: any, member_id: any) {
    // 基于PHP真实逻辑: checkLevelUpgrade
    // PHP原文: $member = (new Member())->where([ ['member_id', '=', $member_id ] ])->with('member_level_data')->field('member_id,growth,member_level')->findOrEmpty()...
const member = this.memberService.where([ ['member_id', '=', member_id ) )).with('member_level_data').field('member_id,growth,member_level').findOrEmpty(];
        if (member.isEmpty()) return true;

        const condition = [
            ['site_id', '=', site_id],
            ['growth', '<=', member.growth ],
        ];
        if (typeof member.member_level_data) && !!member.member_level_data)) {
            condition[] = ['growth', '>', member.member_level_data.growth ];
  }
}
